********************************
* IV and OLS Analysis for MARF *
********************************
use "${build}/iv_fin_marf_w_comp_weights.dta", clear
keep if at_evic_n2_p0 == 1
replace black = 0 if black_miss == 1
replace fem = 0 if fem_miss == 1

*Create datasets we will populate with results and disclosure statistics
cap frame change default
cap frame drop results 
frame create results strL(label) b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evic_all b_judgeamount se_judgeamount 

**************************
* VARIABLE BUILDING *
**************************
gen at_evic_n3_n2_miss = at_evic_n3_n2 == .
replace at_evic_n3_n2 = 0 if at_evic_n3_n2_miss == 1
cap gen fem_hisp = hisp_fem 
gen ageatcase_sq = ageatcase^2
gen ageatcase_cub = ageatcase^3

* building alt 'not at eviction address' that counts missing obs as not at address
foreach rng in   "0_to_0" "1_to_1" "2_to_2" "3_to_3" "4_to_4" "5_to_5" "0_to_1" "1_to_2" "2_to_3" "3_to_4" "4_to_5" "4_to_6" "5_to_6" "n1_to_n1" "n2_to_n2" "n2_to_n1" "n3_to_n3" "n4_to_n4" "n5_to_n4" "n4_to_n3" {
	gen not_at_evic_o_unobs`rng' = not_at_evic`rng'==1 | obs`rng' == 0
	gen state_missing`rng' = state`rng' == .
}


**************************
*Regression specification*
**************************
global cluster_ols="judgeXyear"
global cluster_iv="judgeXyear"
global stem = ""
global instrument="loo_vc_ivsamp100"
global fe="districtXyearXquarter"


*Outcomes
local outcomes_main = "not_at_evic not_at_evic_o_unobs  state avg_pov_rate obs state_missing "

*Deal with missings in the lagged values:
foreach rng in "n1_to_n1" "n2_to_n2" "n2_to_n1" "n3_to_n3" "n4_to_n4" "n5_to_n4" "n4_to_n3" {
	foreach out in `outcomes_main' {
		gen `out'`rng'_miss = `out'`rng' == .
		replace `out'`rng' = -1 if `out'`rng'_miss == 1
	}
}

*Define controls
macro define controls="noprior noprior_miss joint ad_damnum ageatcase ageatcase_sq ageatcase_cub fem black white fem_black fem_white hispanic fem_hisp fem_miss black_miss hispanic_miss nbrhd_pov_rate_5yr nbrhd_med_rent_5yr nbrhd_pov_rate_5yr_miss nbrhd_med_rent_5yr_miss"

*Define judge stringency along money judgment dimension
gen trim_amnt_loo_vc_ivsamp100_s = trimamountloo_vc_ivsamp100/1000
global amnt_instrument = "trim_amnt_loo_vc_ivsamp100_s"	

global main_lags = "not_at_evicn1_to_n1 not_at_evicn2_to_n2 not_at_evicn1_to_n1_miss not_at_evicn2_to_n2_miss"
global n1_lags = "not_at_evicn2_to_n2 not_at_evicn3_to_n3 not_at_evicn2_to_n2_miss not_at_evicn3_to_n3_miss"
global n2_lags = " not_at_evicn3_to_n3 not_at_evicn4_to_n4 not_at_evicn3_to_n3_miss not_at_evicn4_to_n4_miss"
global n3_lags = "not_at_evicn5_to_n4  not_at_evicn5_to_n4_miss"

***********************
* MAIN IV REGRESSIONS *
***********************
gen all=1
gen sample_restriction =.
gen robust_samp=.
gen imp_robust_samp = . 

foreach pop in "all" "black" "fem"   {
	foreach popval of numlist 0 1 {
		if "`pop'"~="all" | `popval'~=0	{	
			qui  foreach var in `outcomes_main'  {
				foreach rng in  "1_to_1" "2_to_2" "3_to_4"  "5_to_6"  "n2_to_n1" {
				if ("`var'" == "cummove_since_evic" & ///
				inlist("`rng'","n1_to_n1", "n2_to_n2", "n3_to_n3", "n2_to_n1")) continue
				
				nois di " ON outcome: `var', pop: `pop'=`popval', time period: `rng' "
				
				if (inlist("`rng'", "n1_to_n1", "n2_to_n2", "n3_to_n3", "n2_to_n1") & "`pop'" != "all") continue
				
				*Set lags to missing if outcome window is a per-period
				if (inlist("`rng'","0_to_0", "1_to_1", "2_to_2", "3_to_3", "4_to_4", "5_to_5") | inlist("`rng'", "0_to_1", "1_to_2", "2_to_3", "3_to_4", "4_to_5", "4_to_6", "5_to_6") )   local lags = " $main_lags "
				if ("`rng'" == "n1_to_n1") local lags = " $n1_lags "
				if ("`rng'" == "n2_to_n2" | "`rng'" == "n2_to_n1") local lags = " $n2_lags "
				if ("`rng'" == "n3_to_n3") local lags = " $n3_lags "
				
				* Dealing with variables that are controls in some regs and outcomes in some regs
				if inlist("`rng'","n1_to_n1", "n2_to_n2", "n3_to_n3","n2_to_n1") {
					replace `var'`rng' = . if `var'`rng'_miss == 1
				}
				
				*Restricting sample across regressions so obs are the same across specs
				ivregress 2sls `var'`rng' i.$fe $controls (evicted=$instrument) if  `pop'==`popval', r cluster($cluster_iv) 
				replace sample_restriction = e(sample)
				
				*Non-evict mean
				sum `var'`rng'   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_mean=r(mean)
				local y_base_sd= r(sd)
				unique pik   if sample_restriction==1 & evicted==0  & `pop'==`popval'
				local y_base_n= r(sum)
				
				sum `var'`rng'   if sample_restriction==1  & `pop'==`popval'
				if "`pop'" == "all" local sd_y_all = r(sd)
				if "`pop'" != "all" local sd_y_all = .
				sum evicted  if sample_restriction==1 & `pop'==`popval'
				if "`pop'" == "all" local sd_evic_all = r(sd)
				if "`pop'" != "all" local sd_evic_all = .
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng'"
				frame post results ("`label'")  (.) (.) (.) (`y_base_mean') (`y_base_sd') (`y_base_n') (`sd_y_all') (`sd_evic_all')  (.) (.)
				
				*OLS with additional controls
				reg  `var'`rng' evicted $controls `lags'  i.$fe   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_ols)
				local b_evicted= _b[evicted]
				local se_evicted=_se[evicted] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Specification: OLS with additional controls" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
			
				*RF with additional controls	
				reg `var'`rng' $instrument $controls `lags' i.$fe   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_iv) 
				local b_evicted= _b[$instrument]
				local se_evicted=_se[$instrument] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Specification: RF with additional controls" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)

				*IV: Baseline with additional controls
				ivregress 2sls `var'`rng' $controls `lags' i.$fe  (evicted=$instrument)   if sample_restriction == 1 & `pop'==`popval', r cluster($cluster_iv) 
				local b_evicted= _b[evicted]
				local se_evicted=_se[evicted] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Specification: IV with additional controls" 	
				frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
					
				if ("`pop'" == "all" & inlist("`var'", "not_at_evic", "avg_pov_rate", "not_at_evic_o_unobs", "obs") ){
					
					*IV: one instrument, one endogenous variable, control for amount stringency
					ivregress 2sls `var'`rng' (evicted=$instrument) i.$fe $amnt_instrument $controls `lags' if sample_restriction == 1 & `pop'==`popval' & joint==1  & judgment_amount~=., r cluster($cluster_iv) 
					local b_evicted= _b[evicted]
					local se_evicted=_se[evicted] 
					local n_obs=e(N)
					local label="Pop: `pop'=`popval', Outcome: `var', Outcome Window: `rng', Robustness: IV with additional controls, one instrument, one endogenous variable, control for amount stringency"
					frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) (.) (.)
					
				}

					if inlist("`rng'","n1_to_n1", "n2_to_n2", "n3_to_n3","n2_to_n1") {
						replace `var'`rng' = -1 if `var'`rng'_miss == 1
					}
				
				}
			}
		}
	}
}


frame results: export delimited using "${Inputs}/Cook_marf_ivols.csv", replace 

import delimited "${Inputs}/Cook_marf_ivols.csv", clear 

split label, parse(",")
rename (label1 label2 label3 label4) (population outcome time_period specification)
rename (b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evic_all) (evic_coeff evic_se obs mean_outcome_non_evic sd_outcome_non_evicted sd_outcome_full_sample sd_evic_full_sample)
drop *judgement*

export delimited using "${Inputs}/Cook_marf_ivols.csv", replace 

